package net.suncaper.mvc.web.controller;

import lombok.var;
import net.suncaper.mvc.common.framework.simplemvc.RequestMapping;
import net.suncaper.mvc.common.utils.JSONUtil;
import net.suncaper.mvc.common.utils.Result;
import net.suncaper.mvc.model.domain.Review;
import net.suncaper.mvc.model.vo.ReviewVO;
import net.suncaper.mvc.common.utils.EntityConverter;
import net.suncaper.mvc.service.IReviewService;
import net.suncaper.mvc.service.impl.ReviewServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 评价控制层
 */
@RequestMapping("/api/review")
public class ReviewController {
    private IReviewService reviewService = new ReviewServiceImpl();

    /**
     * 添加评论
     */
    @RequestMapping("/add.do")
    public void addReview(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            String userId = request.getParameter("userId");
            BigDecimal rating = new BigDecimal(request.getParameter("rating"));
            String content = request.getParameter("content");
            String hotelName = request.getParameter("hotelName");

            // 自动设置当前时间作为评论时间
            Date reviewTime = new Date();

            Review review = new Review();
            review.setReviewTime(reviewTime);
            review.setUserId(userId);
            review.setRating(rating);
            review.setContent(content);
            review.setHotelName(hotelName);

            boolean result = reviewService.addReview(review);
            JSONUtil.printByJSON(response, Result.success(result));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }

    /**
     * 根据酒店获取评价列表
     */
    @RequestMapping("/listByHotel.do")
    public void listByHotel(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            String hotelName = request.getParameter("hotelName");
            var reviews = reviewService.getReviewsByHotelName(hotelName);
            if (reviews != null && !reviews.isEmpty()) {
                java.util.List<ReviewVO> reviewVOList = EntityConverter.convertToReviewVOList(reviews);
                JSONUtil.printByJSON(response, Result.success(reviewVOList));
            } else {
                JSONUtil.printByJSON(response, Result.error("该酒店暂无评价"));
            }
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }

    /**
     * 根据用户ID获取评价列表
     */
    @RequestMapping("/listByUser.do")
    public void listByUser(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            String userId = request.getParameter("userId");
            var reviews = reviewService.getReviewsByUserId(userId);
            if (reviews != null && !reviews.isEmpty()) {
                java.util.List<ReviewVO> reviewVOList = EntityConverter.convertToReviewVOList(reviews);
                JSONUtil.printByJSON(response, Result.success(reviewVOList));
            } else {
                JSONUtil.printByJSON(response, Result.error("该用户暂无评价"));
            }
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }

    /**
     * 查询全部评论
     */
    @RequestMapping("/listAll.do")
    public void listAllReviews(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            java.util.List<Review> reviews = reviewService.getAllReviews();
            if (reviews != null && !reviews.isEmpty()) {
                java.util.List<ReviewVO> reviewVOList = EntityConverter.convertToReviewVOList(reviews);
                JSONUtil.printByJSON(response, Result.success(reviewVOList));
            } else {
                JSONUtil.printByJSON(response, Result.error("暂无评论信息"));
            }
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }

    /**
     * 更新评论
     * 请求参数：reviewTime、content、rating
     */
    @RequestMapping("/update.do")
    public void updateReview(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            String reviewTimeStr = request.getParameter("reviewTime");
            if (reviewTimeStr == null) {
                JSONUtil.printByJSON(response, Result.error("评论时间不能为空"));
                return;
            }
            java.sql.Timestamp reviewTime = java.sql.Timestamp.valueOf(reviewTimeStr);
            String content = request.getParameter("content");
            String ratingStr = request.getParameter("rating");
            BigDecimal rating = null;
            if (ratingStr != null && !ratingStr.isEmpty()) {
                rating = new BigDecimal(ratingStr);
            }
            Review review = new Review();
            review.setReviewTime(reviewTime);
            if (content != null) review.setContent(content);
            if (rating != null) review.setRating(rating);
            boolean result = reviewService.updateReview(review);
            JSONUtil.printByJSON(response, Result.success(result));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }

    /**
     * 删除评论
     * 请求参数：reviewTime
     */
    @RequestMapping("/delete.do")
    public void deleteReview(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            String reviewTimeStr = request.getParameter("reviewTime");
            if (reviewTimeStr == null) {
                JSONUtil.printByJSON(response, Result.error("评论时间不能为空"));
                return;
            }
            java.sql.Timestamp reviewTime = java.sql.Timestamp.valueOf(reviewTimeStr);
            boolean result = reviewService.deleteReview(reviewTime);
            JSONUtil.printByJSON(response, Result.success(result));
        } catch (Exception e) {
            JSONUtil.printByJSON(response, Result.error(e.getMessage()));
        }
    }
}    